﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Model.DatabaseContext;
using Demo.Model.Tables;
namespace Demo.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }
        /// <summary>
        /// To load the angular js
        /// </summary>
        /// <returns></returns>
        public ActionResult IndexVM()
        {
            List<Products> products = null;
            using (ProductContext context = new ProductContext())
            {
                products = context.Product.ToList();
            }
            return Json(products, JsonRequestBehavior.AllowGet);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }
        /// <summary>
        /// To Save Product
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public ActionResult SaveProduct(Products product)
        {
            using (ProductContext context = new ProductContext())
            {
                context.Product.Add(product);
                context.SaveChanges();
            }
            return Json(product);
        }

        public ActionResult DeleteProduct(Guid productID)
        {
            using (ProductContext context = new ProductContext())
            {
                var ObjectToDelete = context.Product.Where(x => x.ID == productID).FirstOrDefault();
                context.Product.Remove(ObjectToDelete);
                context.SaveChanges();
                return Json(ObjectToDelete);
            }
        }

        public ActionResult UpdateProduct(Products product) 
        {
            using (ProductContext context = new ProductContext())
            {
                var ObjectToUpdate = context.Product.Where(x => x.ID == product.ID).FirstOrDefault();
                ObjectToUpdate.Name = product.Name;
                ObjectToUpdate.Price = product.Price;
                ObjectToUpdate.Quantity = product.Quantity;
                ObjectToUpdate.Description = product.Description;
                context.SaveChanges();
                return Json(ObjectToUpdate);
            }
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
